package com.imooc.reader.management.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.imooc.reader.management.entity.Member;
import com.imooc.reader.management.mapper.EvaluationMapper;
import com.imooc.reader.management.mapper.MemberMapper;
import com.imooc.reader.management.mapper.MemberReadStateMapper;
import com.imooc.reader.management.mapper.VerificationCodeMapper;
import com.imooc.reader.management.service.MemberService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

@Service
public class MemberServiceImpl implements MemberService {
    @Resource
    private MemberMapper memberMapper;
    @Resource
    private EvaluationMapper evaluationMapper;
    @Resource
    private MemberReadStateMapper memberReadStateMapper;
    @Resource
    private VerificationCodeMapper verificationCodeMapper;

    @Override
    public IPage<Member> selectByMobile(IPage page, String mobile) {
        if (mobile != null && !mobile.trim().isEmpty()) {
            // 支持模糊查询
            return memberMapper.selectByMobile(page, "%" + mobile + "%");
        } else {
            return memberMapper.selectByMobile(page, null);
        }
    }

    @Override
    public Member deleteMember(Long memberId) {
        Member member = memberMapper.selectById(memberId);
        QueryWrapper wrapper = new QueryWrapper<>();
        wrapper.eq("member_id", memberId);
        memberMapper.deleteById(memberId);
        evaluationMapper.delete(wrapper);
        memberReadStateMapper.delete(wrapper);
        verificationCodeMapper.delete(wrapper);
        return member;
    }
}
